Set your work directory.
We need to load the following library for mapping.
require("ggmap")
## Loading required package: ggmap
## Loading required package: ggplot2
We will cover the following topics:
* making a simple map using google maps API;
* putting markers on a map;
* making choropleths maps based on admin level 2(municipalities) of the Netherlands.
qmap('Arnhem, The Netherlands')
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Arnhem,+The+Netherlands&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Arnhem,%20The%20Netherlands&sensor=false
Using a postal code and zoom
qmap('6835KT, The Netherlands', zoom = 16)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=6835KT,+The+Netherlands&zoom=16&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=6835KT,%20The%20Netherlands&sensor=false
Look at the url which Google maps is using to get the map. We can use the option by using an other command and provide more details.
ggmap(
get_googlemap(
center=c(5.9075781, 51.9560848), #Long/lat of centre, or "arnhem"
zoom=14,
maptype='satellite', #also hybrid/terrain/roadmap
scale = 2), #resolution scaling, 1 (low) or 2 (high)
size = c(600, 600), #size of the image to grab
extent='device', #can also be "normal" etc
darken = 0) #you can dim the map when plotting on top
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=51.956085,5.907578&zoom=14&size=640x640&scale=2&maptype=satellite&sensor=false
We are going to use data from the Refine example.
We load the xls in a new variable test2
test2 <- read.xls("test.xls")
#look at the structure of the data
str(test2)
## 'data.frame': 25 obs. of 11 variables:
## $ company: Factor w/ 4 levels "AKZO","PHILLIPS",..: 2 2 2 2 2 2 1 1 1 1 ...
## $ product: Factor w/ 4 levels "computer","radio",..: 2 2 1 1 1 2 4 4 1 2 ...
## $ aantal : int 5 43 3 34 12 23 43 12 5 34 ...
## $ adres2 : Factor w/ 25 levels "Delfzijlstraat 54",..: 9 10 11 12 13 14 19 20 21 22 ...
## $ plaats : Factor w/ 1 level " arnhem": 1 1 1 1 1 1 1 1 1 1 ...
## $ land : Factor w/ 1 level " the netherlands": 1 1 1 1 1 1 1 1 1 1 ...
## $ lat : num 52 52 52 52 52 ...
## $ long : num 5.91 5.91 5.91 5.91 5.91 ...
## $ gender : Factor w/ 2 levels "m","v": 1 1 1 1 1 1 1 1 1 1 ...
## $ vlet : Factor w/ 3 levels "j.","l.","p.": 3 3 1 3 3 3 3 3 3 3 ...
## $ naam : Factor w/ 20 levels "bansen","Bokken",..: 10 9 7 14 5 6 1 19 3 11 ...
#Now we load the long and lat in seperate data frame
coords <- cbind(Longitude = as.numeric(test2$long), Latitude = as.numeric(test2$lat))
head(coords,5)
## Longitude Latitude
## [1,] 5.907578 51.95608
## [2,] 5.907387 51.95629
## [3,] 5.907549 51.95609
## [4,] 5.907375 51.95629
## [5,] 5.907535 51.95611
Longitude and Latitude are variable 7 and 8.
We load the data in test2 minus 7 and 8 in a separate data frame with the data for the points or markers.
>We need to load a new library:geosphere(and its dependancy library sp), to create the points.
require("geosphere")
## Loading required package: geosphere
## Loading required package: sp
test2.pts <- SpatialPointsDataFrame(coords, test2[, -(7:8)], proj4string = CRS("+init=epsg:4326"))
#let's print the points
plot(test2.pts, pch = "*", col = "darkred")
And now together on map
map <- qmap('Arnhem', zoom = 12, maptype = 'roadmap')
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Arnhem&zoom=12&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Arnhem&sensor=false
map + geom_point(data = test2, aes(x = long, y = lat), color="red", size=3, alpha=0.5)
#With a legend for the map
map + geom_point(data = test2, aes(x = long, y = lat, fill = factor (product)), color="white", pch = 21, size=2, alpha=0.5)
To finalize here are two other examples to show how you can play with the parameters of the map.
#example 1
map <-ggmap(
get_googlemap(
center=c(5.9075781, 51.9560848), #Long/lat of centre, or "arnhem"
zoom=14,
maptype='roadmap', #also hybrid/terrain/roadmap
scale = 2), #resolution scaling, 1 (low) or 2 (high)
size = c(600, 600), #size of the image to grab
extent='device', #can also be "normal" etc
darken = 0) #you can dim the map when plotting on top
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=51.956085,5.907578&zoom=14&size=640x640&scale=2&maptype=roadmap&sensor=false
map + geom_point(data = test2, aes(x = long, y = lat, fill = factor (company)), color="white", pch = 21, size=4, alpha=0.5)
#example 2
map <-ggmap(
get_googlemap(
center=c(5.9075781, 51.9560848), #Long/lat of centre, or "arnhem"
zoom=14,
maptype='roadmap', #also hybrid/terrain/roadmap
scale = 2), #resolution scaling, 1 (low) or 2 (high)
size = c(800, 800), #size of the image to grab
extent='device', #can also be "normal" etc
darken = 0) #you can dim the map when plotting on top
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=51.956085,5.907578&zoom=14&size=640x640&scale=2&maptype=roadmap&sensor=false
map + geom_point(data = test2, aes(x = long, y = lat, fill = factor (company)), color="white", pch = 21, size=4, alpha=0.5)